import os
from typing import Dict, Any
from .file_processor import FileProcessor
from .doc2md.doc_to_markdown import DocToMarkdownProcessor
from .ppt2md.ppt_to_markdown import PPTToMarkdownProcessor
from .pdf2md.pdf_to_markdown import PDFToMarkdownProcessor
from .html2md.html_to_markdown import HtmlToMarkdownProcessor




class UniversalToMarkdownProcessor(FileProcessor):
    def __init__(self, input_filepath: str, output_filepath: str, options: Dict[str, Any] = {}):
        super().__init__(input_filepath, output_filepath, options)
        self.processor = self._get_processor()

    def _get_processor(self):
        _, file_extension = os.path.splitext(self.input_filepath)
        file_extension = file_extension.lower()

        if file_extension in ['.doc', '.docx']:
            return DocToMarkdownProcessor(self.input_filepath, self.output_filepath, self.options)
        elif file_extension in ['.ppt', '.pptx']:
            return PPTToMarkdownProcessor(self.input_filepath, self.output_filepath, self.options)
        elif file_extension == '.pdf':
            return PDFToMarkdownProcessor(self.input_filepath, self.output_filepath, self.options)
        elif file_extension in ['.html', '.htm']:
            return HtmlToMarkdownProcessor(self.input_filepath, self.output_filepath, self.options)
        else:
            raise ValueError(f"Unsupported file format (尚未支持的格式): {file_extension}")

    def _process_file(self):
        return self.processor._process_file()

def test(arg={}):
    # Usage example
    processor = UniversalToMarkdownProcessor("input.docx", "output/converted.md")
    result = processor.process()
    print(result)

    processor = UniversalToMarkdownProcessor("input.pptx", "output/converted.md")
    result = processor.process()
    print(result)

    processor = UniversalToMarkdownProcessor("input.pdf", "output/converted.md")
    result = processor.process()
    print(result)

    processor = UniversalToMarkdownProcessor("input.html", "output/converted.md")
    result = processor.process()
    print(result)

if __name__ == "__main__":
    UniversalToMarkdownProcessor.main()

"""
使用示例：
python3 -m document2llm.md.universal_to_markdown -i input.docx -o output/converted-docx.md
python3 -m document2llm.md.universal_to_markdown -i input.pptx -o output/converted-pptx.md
python3 -m document2llm.md.universal_to_markdown -i input.pdf -o output/converted-pdf.md
python3 -m document2llm.md.universal_to_markdown -i input.html -o output/converted-html.md

或者在Python代码中：

from document2llm.md.universal_to_markdown import UniversalToMarkdownProcessor

processor = UniversalToMarkdownProcessor("input.docx", "output/converted.md")
result = processor.process()
print(result)
"""
